// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design;

import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.IWarehousePluginService;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.core.warehouse.IWarehouseNode;
import org.dengues.designer.ui.process.models.CompProcess;
import org.dengues.model.reports.Reports;
import org.dengues.reports.editor.design.models.ReportsDiagram;
import org.dengues.ui.editors.AbstractEditorInput;
import org.eclipse.core.runtime.IPath;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-1-24 qiang.zhang $
 * 
 */
public class ReportsEditorInput extends AbstractEditorInput {

    private ReportsDiagram reportsDiagram;

    private final Reports reports;

    /**
     * Qiang.Zhang.Adolf@gmail.com ReportsEditorInput constructor comment.
     * 
     * @param reports
     * @param path
     */
    public ReportsEditorInput(Reports reports, IPath path) {
        super(reports.getName(), ENodeCategoryName.REPORTS, path);
        this.reports = reports;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.FileEditorInput#getName()
     */
    @Override
    public String getName() {
        return reports.getName();
    }

    /**
     * Sets the warehouseNode.
     * 
     * @param warehouseNode the warehouseNode to set
     */
    public void setWarehouseNode(IWarehouseNode warehouseNode) {
        if (warehouseNode != null) {
            this.warehouseNode = warehouseNode;
        } else {
            IWarehousePluginService warehouseService = DenguesCorePlugin.getDefault().getWarehousePluginService();
            this.warehouseNode = warehouseService.getWarehouseViewFactory().createWarehouseNodeFromEMF(ENodeCategoryName.PROCESS,
                    parentNode, reports);
        }
    }

    private ReportsDiagram loadReportsDiagram() {
        ReportsDiagram diagram = new ReportsDiagram();
        diagram.setName(getName());
        return diagram;
    }

    /**
     * Getter for process.
     * 
     * @return the process
     */
    public ReportsDiagram getReportsDiagram() {
        if (reportsDiagram == null) {
            reportsDiagram = loadReportsDiagram();
        }
        return this.reportsDiagram;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "saveEMF".
     * 
     * @param process
     */
    public void saveEMF(CompProcess process) {
        WarehouseResourceFactory.save(getReports());
    }

    /**
     * Getter for reports.
     * 
     * @return the reports
     */
    public Reports getReports() {
        return this.reports;
    }

}
